package com.example.spring_blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.example.spring_blog.common.constant.Constants;
import com.example.spring_blog.common.exception.BlogException;
import com.example.spring_blog.common.util.BeanTransUtils;
import com.example.spring_blog.mapper.BlogInfoMapper;
import com.example.spring_blog.pojo.dataobject.BlogInfo;
import com.example.spring_blog.pojo.request.AddBlogRequest;
import com.example.spring_blog.pojo.request.UpdateBlogRequest;
import com.example.spring_blog.pojo.response.BlogInfoResponse;
import com.example.spring_blog.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogInfoMapper blogInfoMapper;

    @Override
    public List<BlogInfoResponse> getList() {
        // 创建查询条件包装器
        QueryWrapper<BlogInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogInfo::getDeleteFlag, Constants.BLOG_NORMAL_NUM);
        List<BlogInfo> blogInfos = blogInfoMapper.selectList(queryWrapper);

        //函数编程 将每个blogInfo都转换成blogInfoResponse，最后通过collect转为List集合
        List<BlogInfoResponse> blogInfoResponseList = blogInfos.stream()
                .map(blogInfo -> BeanTransUtils.trans(blogInfo))
                .collect(Collectors.toList());
        return blogInfoResponseList;
    }

    @Override
    public List<BlogInfoResponse> getBlogsOfUser(Integer userId) {
        // 创建查询条件包装器
        QueryWrapper<BlogInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogInfo::getDeleteFlag, Constants.BLOG_NORMAL_NUM)
                .eq(BlogInfo:: getUserId, userId);
        List<BlogInfo> blogInfos = blogInfoMapper.selectList(queryWrapper);

        //函数编程 将每个blogInfo都转换成blogInfoResponse，最后通过collect转为List集合
        List<BlogInfoResponse> blogInfoResponseList = blogInfos.stream()
                .map(blogInfo -> BeanTransUtils.trans(blogInfo))
                .collect(Collectors.toList());
        return blogInfoResponseList;
    }

    @Override
    public BlogInfoResponse getBlogDetail(Integer blogId) {
        return BeanTransUtils.trans(getBlogInfo(blogId));
    }

    @Override
    public BlogInfo getBlogInfo(Integer blogId) {
        QueryWrapper<BlogInfo> queryWrapper = new QueryWrapper<>();
        // 查询没有被删除的并且博客id为blogId的博客
        queryWrapper.lambda().eq(BlogInfo::getDeleteFlag, Constants.BLOG_NORMAL_NUM)
                .eq(BlogInfo::getId, blogId);
        // 对应一篇博客
        return blogInfoMapper.selectOne(queryWrapper);
    }

    @Override
    public Boolean addBlog(AddBlogRequest addBlogRequest) {
        BlogInfo blogInfo = new BlogInfo();
        BeanUtils.copyProperties(addBlogRequest, blogInfo);
        try {
            int insert = blogInfoMapper.insert(blogInfo);
            if (insert > 0) {
                return true;
            }
        } catch (Exception e) {
            throw new BlogException("博客插入失败，请联系管理员！");
        }
        return false;
    }

    @Override
    public Boolean updateBlog(UpdateBlogRequest updateBlogRequest) {
        BlogInfo blogInfo = new BlogInfo();
        BeanUtils.copyProperties(updateBlogRequest, blogInfo);
        try {
            int i = blogInfoMapper.updateById(blogInfo);
            if (i > 0) {
                return true;
            }
        } catch (Exception e) {
            log.error("更新失败, e : "+ e);
            throw new BlogException("更新失败，请联系管理员！");
        }
        return false;
    }

    @Override
    public Boolean deleteBlog(Integer blogId) {
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(Constants.BLOG_DELETE_NUM);
        try {
            int i = blogInfoMapper.updateById(blogInfo);
            if (i > 0) {
                return true;
            }
        } catch (Exception e) {
            log.error("删除失败, e : "+ e);
            throw new BlogException("删除失败，请联系管理员！");
        }
        return false;
    }


}
